package com.fun.demo.fun;

import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import java.util.Hashtable;

public class ADAuth {
    public static void main(String[] args) {
        String username= "hey@kypro.com";
        String pwd = "%usqfKrv5Z";
        String path = "ldap://kypro.com:389";

        Hashtable<String, String> env = new Hashtable<String, String>();
        env.put(Context.SECURITY_AUTHENTICATION, "simple");
        env.put(Context.SECURITY_PRINCIPAL, username);
        env.put(Context.SECURITY_CREDENTIALS, pwd);
        env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
        env.put(Context.PROVIDER_URL, path);

        DirContext ctx = null;
        try {
            ctx = new InitialDirContext(env);
            System.out.println("Binding ok.");
        } catch (NamingException err) {
           err.printStackTrace();
        } finally {
            try {
                if (ctx != null)
                    ctx.close();
            } catch (NamingException ex) {
               ex.printStackTrace();
            }
        }
    }
}
